package com.szpx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.Goods;
import com.szpx.mapper.GoodsMapper;
import com.szpx.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.util.StringUtil;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author June
 * @version 1.0.0
 * @title GoodsServiceImpl
 * @create 2024/3/28 16:34
 * @description
 **/
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品信息
     *
     * @return
     */
    @Override
    public List<Goods> findAll() {
        List<Goods> GoodsList = goodsMapper.selectAll();
        return GoodsList;
    }

    /**
     * 根据id来查询商品信息
     *
     * @param gid
     * @return
     */
    @Override
    public Goods findById(Integer gid) {
        return goodsMapper.selectByGid(gid);
    }

    /**
     * 添加商品信息
     *
     * @param goods
     */
    @Override
    public void addGoods(Goods goods) {
        goodsMapper.insertSelective(goods);
    }

    /**
     * 修改商品信息
     *
     * @param goods
     */
    @Override
    public void updateGoods(Goods goods) {
        goodsMapper.update(goods);
    }

    /**
     * 删除商品信息
     *
     * @param id
     */
    @Override
    public void deleteGoods(Integer id) {
        goodsMapper.deleteById(id);
    }

    /**
     * 多条件查询商品信息
     *
     * @param goods
     * @return
     */
    @Override
    public List<Goods> findList(Goods goods) {
        Example example = createExample(goods);
        return goodsMapper.selectByExample(example);
    }

    /**
     * 分页查询商品信息
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Goods> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<Goods>(goodsMapper.selectAll());
    }

    /**
     * 多条件分页查询商品信息
     *
     * @param goods
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Goods> findPage(Goods goods, int page, int size) {
        Example example = createExample(goods);
        PageHelper.startPage(page, size);
        return new PageInfo<Goods>(goodsMapper.selectByExample(example));
    }

    /**
     * 构建Example多条件的对象
     * @param goods
     * @return
     */
    private Example createExample(Goods goods){
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        if(goods!=null){
            //商品名称
            if(!StringUtil.isEmpty(goods.getTitle())){
                criteria.andLike("title","%"+goods.getTitle()+"%");
            }
            //商品标题
            if(!StringUtil.isEmpty(goods.getShop())){
                criteria.andEqualTo("shop",goods.getShop());
            }
            //商品类型
            if(!StringUtil.isEmpty(goods.getType())){
                criteria.andEqualTo("type",goods.getType());
            }
        }
        return example;
    }
}
